unit smClassWorld;

interface

uses
  Lua;

const
  luaCLASS_WORLD: AnsiString = 'World';

  procedure OpenClass(const LuaState: lua_State);

implementation

uses
  smLuaUtils, smPlayer, smWorld;

function _WorldLoadLocation(L: lua_State): Integer; cdecl; forward;
function _WorldUnloadLocation(L: lua_State): Integer; cdecl; forward;
function _WorldDate(L: lua_State): Integer; cdecl; forward;
function _WorldTime(L: lua_State): Integer; cdecl; forward;
function _WorldToday(L: lua_State): Integer; cdecl; forward;

const
  WORLD_FUNCS: array [0..5] of luaL_reg = (
    (name: 'LoadLocation'; func: _WorldLoadLocation),
    (name: 'UnloadLocation'; func: _WorldUnloadLocation),
    (name: 'Date'; func: _WorldDate),
    (name: 'Time'; func: _WorldTime),
    (name: 'Today'; func: _WorldToday),
    (name: nil; func: nil)
  );

procedure OpenClass;
begin
  // Register class "World" and corresponding functions
  SetClass(luaCLASS_WORLD, @WORLD_FUNCS[0]);

  // Some globals
  SetFieldString(luaCLASS_WORLD, 'Name', '');
  SetFieldString(luaCLASS_WORLD, 'Location', '');
end;

function _WorldLoadLocation;
begin
  Player.World.StartLoading('');
  Result := 0;
end;

function _WorldUnloadLocation;
begin
  if Player.World.State = wsReady then
    Player.World.Location.Unload();
  Result := 0;
end;

function _WorldDate;
begin
  try
    with Player.World.Time^ do
    begin
      lua_pushinteger(L, Year);
      lua_pushinteger(L, Month);
      lua_pushinteger(L, Day);
    end;
    Result := 3;

  except
    Result := 0;
  end;
end;

function _WorldTime;
begin
  try
    with Player.World.Time^ do
    begin
      lua_pushinteger(L, Time div 60);
      lua_pushinteger(L, Time mod 60);
    end;
    Result := 2;

  except
    Result := 0;
  end;
end;

function _WorldToday;
begin
  try
    lua_pushstring(L, PAnsiChar(Player.World.Time.ToString));
    Result := 1;

  except
    Result := 0;
  end;
end;

end.
